#!/bin/bash

if [ $# != 1 ]; then
	echo "Usage: bash create_env.sh <env_name>"
	echo "[ERROR] Invalid arguments: $@"
	exit 1
fi
env_name=$1

set -e

conda create -n ${env_name} python=3.8
source activate ${env_name}

huawei_pypi="https://repo.huaweicloud.com/repository/pypi/simple"
tsinghua_pypi="https://pypi.tuna.tsinghua.edu.cn/simple"


# ================== install foundational components ================
pip install torch==1.13.0 torchvision -i ${huawei_pypi}
pip install -U openmim -i ${huawei_pypi}
mim install mmengine==0.7.2 -i ${tsinghua_pypi}
mim install mmcv==2.0.0 -i ${huawei_pypi}
mim install mmdeploy==v1.0.0 -i ${huawei_pypi}
pip install decorator==5.1.1 -i ${huawei_pypi}
pip install attrs==22.2.0 -i ${huawei_pypi}


# ======================= install mmdetection =======================
# To obtain config easier, we install mmdet by source codes. 
# Here you can choose the way to install mmdet as per your needs.
git clone -b v3.0.0 https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e . -i ${huawei_pypi}
cd ..


# ======================== install onnx tool ========================
git clone https://gitee.com/ascend/msadvisor.git
cd msadvisor/auto-optimizer
pip install --upgrade pip -i ${huawei_pypi}
pip install wheel -i ${huawei_pypi}
pip install . -i ${huawei_pypi}
cd ../../
rm -rf msadvisor


# ======================= install infer tools =======================
# install aclruntime based on the platform
arch=`arch`
if [[ $arch = "x86_64" ]]; then
	wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/aclruntime-0.0.2-cp38-cp38-linux_x86_64.whl
	pip install aclruntime-0.0.2-cp38-cp38-linux_x86_64.whl
	rm aclruntime-0.0.2-cp38-cp38-linux_x86_64.whl
elif [[ $arch = "aarch64" ]]; then
	wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/aclruntime-0.0.2-cp38-cp38-linux_aarch64.whl
	pip install aclruntime-0.0.2-cp38-cp38-linux_aarch64.whl
	rm aclruntime-0.0.2-cp38-cp38-linux_aarch64.whl
else
	echo "aclruntime not supported the platform: ${arch}"
	exit 1
fi
# install ais_bench
wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ais_bench-0.0.2-py3-none-any.whl
pip install ais_bench-0.0.2-py3-none-any.whl
rm ais_bench-0.0.2-py3-none-any.whl
